<template>
  <div class="header-container">
    <div class="l-content">
      <el-button
          @click="handleMenu"
          plain
          icon="el-icon-menu"
          size="mini"
      ></el-button>
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item
            v-for="item in tags"
            :key="item.path"
            :to="{ path: item.path }"
            :class="{ 'active-breadcrumb': isActive(item.path) }"
        >{{ item.label }}</el-breadcrumb-item
        >
      </el-breadcrumb>
    </div>
    <div class="r-content">
      <a
          href="https://dms.cloud.tencent.com/#/login?dbType=cynosdbmysql&region=ap-shanghai&instanceId=cynosdbmysql-hb6l1xuq"
          class="manager"
          size="mini"
          style="color: #ffffff; margin-right: 20px"
      >Super</a
      >
      <el-dropdown trigger="click" size="mini">
        <span>
          <h3 class="manager" style="color: #fff">管理员</h3>
        </span>
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item @click="handleLogout">退出</el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
    </div>
  </div>
</template>
<script>
import { mapState } from "vuex";
export default {
  name: "CommonHeader",

  methods: {
    handleMenu() {
      this.$store.commit("CollapseMenu");
    },
    isActive(path) {
      return this.$route.path === path;
    },
    handleLogout() {
      const token = localStorage.getItem("token");

      // 向后端发送登出请求
      this.$http
          .post(
              "/emp/logout",
              {},
              {
                headers: {
                  token: token,
                },
              }
          )
          .then((response) => {
            if (response.body.code === 200) {
              // 清除本地存储中的token
              this.$store.commit("clearToken");

              // 导航到登录页面
              this.$router.push({ path: "/login" });

              // 显示退出成功的消息
              this.$message.success("退出成功");
            } else {
              this.$message.error(response.body.msg || "退出失败");
            }
          })
          .catch((error) => {
            this.$message.error("退出失败：" + error.message);
          });
    },
  },
  computed: {
    ...mapState({
      tags: (state) => state.tab.tabsList,
    }),
  },
};
</script>
<style lang="less" scoped>
.header-container {
  display: flex;
  height: 100%;
  justify-content: space-between;
  align-items: center;
}
.l-content {
  display: flex;
  align-items: center;
  .el-button {
    margin-right: 20px;
  }
  .el-breadcrumb ::v-deep .el-breadcrumb__inner {
    color: #dfdbd6 !important;
    font-weight: 400 !important;
  }
  .active-breadcrumb ::v-deep .el-breadcrumb__inner {
    color: #ea8d13 !important;
    font-weight: 800 !important;
  }
}
.r-content {
  .manager {
    width: 50px;
    height: 20px;
  }
}
</style>
